import requests
import json
import re
from bs4 import BeautifulSoup
from downloadthread import downloadThread
from threadManager import ThreadManager

# return a list of links from the given board
def downloadBoard(board, board_name):
    if len(board) <= 19:
        raise ValueError("Invalid URL")

    board = board.replace("html", "json")
    board = str(board.strip())

    # try request if failed return -1
    board_request = requests.get(board)
    if board_request.status_code != 200:
        return -1

    board_json = json.loads(board_request.text)
    links = []
    for i in board_json:
        for j in i["threads"]:
            links.append(
                "https://8ch.net" + str(board_name) + "res/" + str(j["no"]) + ".html"
            )

    return links


def downloadAndSave(currentBoard):

    boardJson = downloadBoard("https://8ch.net" + currentBoard + "catalog.html", currentBoard)
    
    threadsComplete = 0
    total = len(boardJson)

    mythreads = []
    for i, thread in enumerate( boardJson ):
        # get json
        json = downloadThread(thread)

        # add all posts to thread manager
        mythreads.append(ThreadManager(currentBoard))
        threadsComplete+=1
        print(f"fetching thread {threadsComplete}/{total} board:{currentBoard}")
        if json != -1:
            for post in json["posts"]:
                mythreads[i].addPost(post)
            mythreads[i].save()
            # print("Loaded thread\n" + str(mythreads[i][0]))